package com.example.emos.api.controller;

import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.common.util.R;
import com.example.emos.api.controller.form.*;
import com.example.emos.api.db.pojo.TbModel;
import com.example.emos.api.db.pojo.TbPackManger;
import com.example.emos.api.service.ModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/model")
@Tag(name = "ModelController",description = "型号管理")
public class ModelController {
    @Autowired
    private ModelService modelService;

    @PostMapping("/searchModelByPage")
    @Operation(summary = "型号分页查询")
    public R searchModelByPage(@Valid @RequestBody SearchModelByPageForm form){

        int page= form.getPage();
        int length= form.getLength();
        int start=(page-1)*length;
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start",start);

        PageUtils pageUtils=modelService.searchModelByPage(param);
        return R.ok().put("page",pageUtils);
    }
    @PostMapping("/searchModelAll")
    @Operation(summary = "型号查询")
    public R searchModelAll(@Valid @RequestBody SearchModelAllForm form){

//        int page= form.getPage();
//        int length= form.getLength();
//        int start=(page-1)*length;
        int start=0;
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start",start);
        param.put("length",500);

        PageUtils pageUtils=modelService.searchModelByPage(param);
        return R.ok().put("page",pageUtils);
    }
    @PostMapping("/insert")
//    @SaCheckPermission(value = {"ROOT", "USER:INSERT"}, mode = SaMode.OR)
    @Operation(summary = "添加型号")
    public R insert(@Valid @RequestBody InsertModelForm form){
        TbModel tbModel= JSONUtil.parse(form).toBean(TbModel.class);
        tbModel.setStatus(1);
//        user.setRole(JSONUtil.parseArray(form.getRole()).toString());
        tbModel.setCreateTime(new Date());
        int rows=modelService.insertModel(tbModel);
        if(rows>0){

            return R.ok().put("rows",rows);
        }else {
            return R.error("型号 或者 型号Id 不可重复");
        }
    }


    @PostMapping("/update")
//    @SaCheckPermission(value = {"ROOT", "USER:INSERT"}, mode = SaMode.OR)
    @Operation(summary = "修改型号")
    public R update(@Valid @RequestBody UpdateModelForm form){
        TbModel tbModel= JSONUtil.parse(form).toBean(TbModel.class);
//        tbModel.setId(Integer.parseInt(form.getMId()));
//        tbModel.setStatus(1);
//        user.setRole(JSONUtil.parseArray(form.getRole()).toString());
        tbModel.setCreateTime(new Date());
        int rows=modelService.updateByPrimaryKeySelective(tbModel);
        return R.ok().put("rows",rows);
    }
}


